package icu.sehnen.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.sehnen.common.result.CommonResult;
import icu.sehnen.product.entity.MyPage;
import icu.sehnen.product.entity.ProCategory;
import icu.sehnen.product.entity.Product;
import icu.sehnen.product.service.IProCategoryService;
import icu.sehnen.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    IProductService productService;
    @Autowired
    IProCategoryService proCategoryService;

    @GetMapping("/list")
    public CommonResult findAll(Integer pageNum, Integer pageSize,Product product){
        MyPage myPage = new MyPage(pageNum, pageSize);
        IPage<Product> productModelList=productService.findAll(myPage,product);
        return  CommonResult.success(productModelList);

    }
    @GetMapping("/category")
    public CommonResult getCategory(){
        List<ProCategory> proCategoryModelList= proCategoryService.list();
        return  CommonResult.success(proCategoryModelList);
    }
    @PostMapping("/delete/{id}")
    public  CommonResult delete(@PathVariable Long id){
        productService.removeById(id);
        return  CommonResult.success();
    }
    @GetMapping("/find/{id}")
    public CommonResult findProductById(@PathVariable Long id){
        Product productModel=productService.getProductById(id);
        return  CommonResult.success(productModel);
    }

    @PostMapping("/update")
    public  CommonResult updateProductById(Product productModel){
        productService.updateById(productModel);
        return  CommonResult.success();

    }
    @PostMapping("/add")
    public  CommonResult add(Product product){
        productService.save(product);
        return CommonResult.success();

    }
}
